﻿<?php 
	/**
	 *	新浪t.cn短网址接口(PHP)
	 *  详细介绍查看新浪开放平台文档：http://open.t.sina.com.cn/wiki/index.php/Short_url/expand
	 * 	程序代码来自标点符：http://www.biaodianfu.com/sina-shortener-url-api-php.html
	*/
	/**
	 *	生成t.cn短网址的函数
	 *	
	*/
	function shortenSinaUrl($long_url){
		$apiKey='API-KEY';//api key请登入新浪开放平台申请；http://open.t.sina.com.cn/
		$apiUrl='http://api.t.sina.com.cn/short_url/shorten.json?source='.$apiKey.'&url_long='.$long_url;
		$curlObj = curl_init();
		curl_setopt($curlObj, CURLOPT_URL, $apiUrl);
		curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curlObj, CURLOPT_HEADER, 0);
		curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
		$response = curl_exec($curlObj);
		curl_close($curlObj);
		$json = json_decode($response);
		return $json[0]->url_short;
	}
	
	/**
	 *	还原短网址的函数
	*/
	function expandSinaUrl($short_url){
		$apiKey='API-KEY';
		$apiUrl='http://api.t.sina.com.cn/short_url/expand.json?source='.$apiKey.'&url_short='.$short_url;
		$curlObj = curl_init();
		curl_setopt($curlObj, CURLOPT_URL, $apiUrl);
		curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curlObj, CURLOPT_HEADER, 0);
		curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
		$response = curl_exec($curlObj);
		curl_close($curlObj);
		$json = json_decode($response);
		return $json[0]->url_long;
	}
?>